/**
 * 
 */
package com.imprenta.ejb;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;


import com.hibernate.util.HibernateUtil;


/**
 * @author usuario
 *
 */
public class EjbAutentication {
	
	@SuppressWarnings("rawtypes")
	public static boolean ValidateUser(String userName, String password) {
		boolean isValid = false;
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String Qry = "from Login where username = :username and  password = :pwd";
			Query q = session.createQuery(Qry);
			q.setParameter("username", userName);
			q.setParameter("pwd", password);
			
			List l = q.list();
			if (l.size() > 1) {
				isValid = false;
			}else if (l.size() <= 0) {
				isValid = false;
			}else {
				isValid = true;
			}
			
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if (session != null) {
				session.close();
			}			
		}
		
		return isValid;
	}//fin validateuser
	
	@SuppressWarnings("rawtypes")
	public static int getLogin(String userName, String password){
		
		int id=0;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			//Cliente c = new Cliente();
			
			String Qry = "select l.idlogin,l.username, l.rol from Login l where l.username = :username and  l.password = :pwd";
			Query q = session.createQuery(Qry);
			q.setParameter("username", userName);
			q.setParameter("pwd", password);
			
			List l = q.list();
			for (int i = 0; i < l.size(); i++) {
				Object[] items = (Object[]) l.get(i);
				System.out.println(items[0].toString());System.out.println(items[1].toString());System.out.println(items[2].toString());
				id = Integer.parseInt(items[0].toString());
			}			
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if (session != null) {
				session.close();
			}			
		}
		return id;
	}
	@SuppressWarnings("rawtypes")
	public static int getInfoCliente(int idLogin){

		Session session = HibernateUtil.getSessionFactory().openSession();
		int id=0;
		try {
			
			String Qry = "select c.clienteid, c.nombre from Cliente c inner join c.login l where l.idlogin = :id";
			Query q = session.createQuery(Qry);
			q.setParameter("id", idLogin);
			
			List l = q.list();
			for (int i = 0; i < l.size(); i++) {
				Object[] items = (Object[]) l.get(i);
				System.out.println(items[0].toString());System.out.println(items[1].toString());
				id = Integer.parseInt(items[0].toString());
			}			
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if (session != null) {
				session.close();
			}			
		}
		return id;		
	}

	@SuppressWarnings("rawtypes")
	public static String getUserRol(String userName, String password) {
		
		String rol = "indefinido";
	
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String Qry = "select rol from Login where username = :username and  password = :pwd";
			Query q = session.createQuery(Qry);
			q.setParameter("username", userName);
			q.setParameter("pwd", password);
			
			List l = q.list();
			if (l.size() > 1) {
				rol = "indefinido";
			}else if (l.size() <= 0) {
				rol = "indefinido";
			}else {
				rol = q.list().get(0).toString();
			}
			
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if (session != null) {
				session.close();
			}			
		}
		
		return rol;
	}//fin getrol
}
